//
//  XHRMedcalRecordCell.m
//  快医
//
//  Created by 胥鸿儒 on 16/7/6.
//
//

#import "XHRMedicalRecordCell.h"
#import "XHRMedicalRecordItem.h"
@interface XHRMedicalRecordCell()
/**时间标签*/
@property(nonatomic,weak)UILabel *timeLabel;
/**疾病类型*/
@property(nonatomic,weak)UILabel *illTypeLabel;
/**基本类型*/
@property(nonatomic,weak)UILabel *baseTypeLabel;
/**详细信息*/
@property(nonatomic,weak)UILabel *detailDescription;
/**配图*/
@property(nonatomic,weak)UIImageView *iconView;
/**左边圆圈*/
@property(nonatomic,weak)UIImageView *radiousView;
/**左边线*/
@property(nonatomic,weak)UIView *lineView;
@end

@implementation XHRMedicalRecordCell

+ (instancetype)medicalRecordCellWithTabelView:(UITableView *)tableView
{
    static NSString *const medicalRecordID = @"medicalRecordID";
    XHRMedicalRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:medicalRecordID];
    if (!cell) {
        cell = [[self alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:medicalRecordID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
    }
    return self;
}

#pragma mark -
#pragma mark - 重写setter方法
- (void)setMedicalRecordItem:(XHRMedicalRecordItem *)medicalRecordItem
{
    _medicalRecordItem = medicalRecordItem;
    
    self.detailDescription.text = medicalRecordItem.detailDescription;
    self.timeLabel.text = medicalRecordItem.time;
    
    if (!medicalRecordItem.image) {

        [self.iconView removeFromSuperview];
        [self.illTypeLabel remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.timeLabel.bottom).offset(XHRMedicalRecordMargin);
            make.leading.equalTo(self.timeLabel).offset(XHRMedicalRecordMargin);
        }];
    }
    else
    {
        self.iconView.hidden = NO;
        self.iconView.image = medicalRecordItem.image;
        [self.illTypeLabel remakeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.iconView.trailing).offset(1.5 * XHRMedicalRecordMargin);
            make.top.equalTo(self.timeLabel.bottom).offset(XHRMedicalRecordMargin);
        }];
    }
    NSString *illTypeString = [NSString stringWithFormat:@"疾病类型:  %@",medicalRecordItem.illType];
    NSString *illBaseString = [NSString stringWithFormat:@"基本类型:  %@",medicalRecordItem.baseType];
    
    NSRange range = NSMakeRange(illTypeString.length - medicalRecordItem.illType.length ,medicalRecordItem.illType.length);
    self.illTypeLabel.attributedText = [self setupAttringbutedStringWith:illTypeString range:range];
    
    NSRange range2 = NSMakeRange(illBaseString.length - medicalRecordItem.baseType.length , self.medicalRecordItem.baseType.length);
    self.baseTypeLabel.attributedText = [self setupAttringbutedStringWith:illBaseString range:range2];
    
    [self.contentView layoutIfNeeded];
}
- (NSAttributedString *)setupAttringbutedStringWith:(NSString *)string range:(NSRange)range
{
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc]initWithString:string];
    [attributeString beginEditing];
    [attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:range];
    [attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:range];
    
    [attributeString endEditing];
    return attributeString.copy;
}
#pragma mark -
#pragma mark - layoutSubviews
- (void)layoutSubviews
{

    [super layoutSubviews];
    
    [self.radiousView makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(XHRMedicalRecordMargin);
        make.leading.offset(2 * XHRMedicalRecordMargin);
        make.height.width.equalTo(15);
    }];
}

#pragma mark -
#pragma mark - 懒加载控件
- (UIImageView *)radiousView
{
    if (!_radiousView) {
        UIImageView *radiousView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"illness_rb_img_sel.png"]];
        _radiousView = radiousView;
        [self.contentView addSubview:radiousView];
        //MARK:布局左边的线
        [self.lineView makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(radiousView);
            make.width.equalTo(2);
            make.top.equalTo(radiousView.bottom);
            make.bottom.offset(XHRMedicalRecordMargin);
        }];
        //MARK:布局时间label
        [self.timeLabel makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(XHRMedicalRecordMargin);
            make.leading.equalTo(self.radiousView.trailing).offset(2 * XHRMedicalRecordMargin);
        }];
    }
    return _radiousView;
}
- (UIView *)lineView
{
    if (!_lineView) {
        UIView *lineView = [[UIView alloc]init];
        lineView.backgroundColor = XHRColorWithRGB(25, 170, 187);
        _lineView = lineView;
        [self.contentView addSubview:lineView];
    }
    return _lineView;
}
- (UILabel *)timeLabel
{
    if (!_timeLabel) {
        UILabel *label = [[UILabel alloc]init];
        label.font = [UIFont systemFontOfSize:15];
        label.textColor = [UIColor lightGrayColor];
        [self.contentView addSubview:label];
        _timeLabel = label;
    }
    return _timeLabel;
}
- (UILabel *)illTypeLabel
{
    if (!_illTypeLabel) {
        UILabel *label = [[UILabel alloc]init];
        _illTypeLabel = label;
        [self.contentView addSubview:label];
    }
    return _illTypeLabel;
}
- (UILabel *)baseTypeLabel
{
    if (!_baseTypeLabel) {
        UILabel *label = [[UILabel alloc]init];
        _baseTypeLabel = label;
        [self.contentView addSubview:label];
        
        [label makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.illTypeLabel.bottom).offset(XHRMedicalRecordMargin);
            make.leading.equalTo(self.illTypeLabel);
        }];
    }
    return _baseTypeLabel;
}
- (UILabel *)detailDescription
{
    if (!_detailDescription) {
        UILabel *label = [[UILabel alloc]init];
        label.font = [UIFont systemFontOfSize:14];
        label.numberOfLines = 0;
        label.textColor = [UIColor lightGrayColor];
        _detailDescription = label;
        [self.contentView addSubview:label];
        
        [label makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.illTypeLabel);
            make.trailing.offset(-XHRMedicalRecordMargin);
            make.top.equalTo(self.baseTypeLabel.bottom).offset(XHRMedicalRecordMargin);
        }];
    }
    return _detailDescription;
}
- (UIImageView *)iconView
{
    if (!_iconView) {
        UIImageView *iconView = [[UIImageView alloc]init];
        [self.contentView addSubview:iconView];
        _iconView = iconView;
        [iconView makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.timeLabel.bottom).offset(XHRMedicalRecordMargin);
            make.leading.equalTo(self.timeLabel);
            make.width.height.equalTo(80);
        }];
    }
    return _iconView;
}
@end
